import Vue from 'vue'
import VueRouter from 'vue-router'
import LoadingView from '@/views/LoadingView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '*',
    name: "error",
    component: () => import(/* webpackChunkName: "error" */ '../views/ErrorView.vue'),
    meta: {
      title: "404错误"
    }
  },
  {
    path: "/",
    redirect: "/loading"
  },
  {
    path: "/loading",
    name: "loading",
    component: LoadingView,
    meta: {
      title: "登录"
    }
  },
  {
    path: "/create",
    name: "create",
    component: () => import(/* webpackChunkName: "create" */ "../views/CreateView.vue"),
    meta: {
      title: "注册"
    }
  },
  {
    path: '/home',
    name: 'home',
    redirect: '/home/state',
    component: () => import(/* webpackChunkName: "home" */ '../views/HomeView.vue'),
    meta: {
      is: true,
      title: "首页"
    },
    children: [{
      path: "state",
      name: "state",
      redirect: '/home/state/statecom',
      component: () => import(/* webpackChunkName: "state" */ "../views/StateView.vue"),
      meta: {
        is: true,
        title: "首页"
      },
      children: [{
        path: "statecom",
        name: "statecom",
        component: () => import(/* webpackChunkName: "statecom" */ "../components/StateCom.vue"),
        meta: {
          is: true,
          title: "首页"
        },
      }, {
        path: "web",
        name: "web",
        component: () => import(/* webpackChunkName: "web" */ "../components/WebCom.vue"),
        meta: {
          is: true,
          title: "首页"
        },
      }, {
        path: "province",
        name: "province",
        component: () => import(/* webpackChunkName: "province" */ "../components/ProvinceCom.vue"),
        meta: {
          is: true,
          title: "首页"
        },
      }]
    }, {
      path: "business",
      name: "business",
      component: () => import(/* webpackChunkName: "business" */ '../views/BusinessView.vue'),
      meta: {
        is: true,
        title: "企业管理"
      },
      children:[{
        path:"",
        name:""
      }]
    }, {
      path: "permission",
      name: "permission",
      component: () => import(/* webpackChunkName: "permission" */ "../views/PermissionView.vue"),
      meta: {
        is: true,
        title: "首页"
      },
    }]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue'),
    meta: {
      title: "关于"
    }
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.is) {
    if (localStorage.getItem('age')) {
      next()
    }
  } else {
    next()
  }
})

router.afterEach((to) => {
  document.title = to.meta.title
})

export default router
